package com.bfsoft.springbootinit.util;


import com.bfsoft.springbootinit.config.login.UserDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class RedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private final static String Process_PREFIX = "process:";


    private final static String TOKEN_PREFIX = "user:";

    // 保存用户信息到 Redis
    public void saveUser(String token, UserDetail userInfo) {
        redisTemplate.opsForValue().set(TOKEN_PREFIX + token, JSON.toJSONString(userInfo), 1, TimeUnit.DAYS);  // 设置过期时间为1天
    }

    // 从 Redis 获取用户信息
    public UserDetail getUser( String token) {
        return JSON.parseObject(redisTemplate.opsForValue().get(TOKEN_PREFIX + token), UserDetail.class);
    }

    // 删除用户信息
    public void deleteUser(String token) {
        redisTemplate.delete(TOKEN_PREFIX + token);
    }



}

